package dto

import (
	"go-admin/app/admin/models"
	"go-admin/common/dto"
	common "go-admin/common/models"
)

type AppGetPageReq struct {
	dto.Pagination `search:"-"`
	Name           string               `form:"name"  search:"type:contains;column:name;table:app" comment:"流水线名称"`
	Environments   []models.Environment `form:"environments"  search:"type:exact;column:environment_id;table:app" comment:"环境"`
	AppOrder
}

type AppOrder struct {
	Id           string               `form:"idOrder"  search:"type:order;column:id;table:app"`
	Name         string               `form:"nameOrder"  search:"type:order;column:name;table:app"`
	Environments []models.Environment `form:"environmentsOrder"  search:"type:order;column:environment_id;table:app"`
	CodeRepo     string               `form:"codeRepoOrder"  search:"type:order;column:code_repo;table:app"`
	ImageUrl     string               `form:"imageUrlOrder"  search:"type:order;column:image_url;table:app"`
	ImageProject string               `form:"imageProjectOrder"  search:"type:order;column:image_project;table:app"`
	Notify       string               `form:"notifyOrder"  search:"type:order;column:notify;table:app"`
	Status       string               `form:"statusOrder"  search:"type:order;column:status;table:app"`
	Remark       string               `form:"remarkOrder"  search:"type:order;column:remark;table:app"`
	CreatedAt    string               `form:"createdAtOrder"  search:"type:order;column:created_at;table:app"`
	UpdatedAt    string               `form:"updatedAtOrder"  search:"type:order;column:updated_at;table:app"`
	DeletedAt    string               `form:"deletedAtOrder"  search:"type:order;column:deleted_at;table:app"`
	CreateBy     string               `form:"createByOrder"  search:"type:order;column:create_by;table:app"`
	UpdateBy     string               `form:"updateByOrder"  search:"type:order;column:update_by;table:app"`
}

func (m *AppGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type AppInsertReq struct {
	Id                int    `json:"-" comment:"编码"` // 编码
	Name              string `json:"name" comment:"应用名称"`
	AppType           string `json:"appType" comment:"应用类型"`
	Port              string `json:"port"`
	EnvironmentIds    []int  `json:"environmentIds" comment:"环境列表"`
	CodeRepo          string `json:"codeRepo" comment:"代码仓库地址"`
	ApolloConfigURL   string `json:"apolloConfigURL" comment:"阿波罗地址"`
	ApolloServiceName string `json:"apolloServiceName" comment:"阿波罗服务名称"`
	CpuRequest        string `json:"cpuRequest"`
	CpuLimit          string `json:"cpuLimit"`
	MemRequest        string `json:"memRequest"`
	MemLimit          string `json:"memLimit"`
	Status            string `json:"status" comment:"状态"`
	Remark            string `json:"remark" comment:"备注"`
	DockerfileName    string `json:"dockerfileName"`
	common.ControlBy
}

func (s *AppInsertReq) Generate(model *models.App) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	if len(s.EnvironmentIds) > 0 {
		var envs []models.Environment
		for _, id := range s.EnvironmentIds {
			envs = append(envs, models.Environment{
				Model: common.Model{
					Id: id,
				},
			})
		}
		model.Environments = envs
	}

	model.Name = s.Name
	model.AppType = s.AppType
	model.Port = s.Port
	model.CodeRepo = s.CodeRepo
	model.Status = s.Status
	model.Remark = s.Remark
	model.CreateBy = s.CreateBy // 添加这而，需要记录是被谁创建的
}

func (s *AppInsertReq) GetId() interface{} {
	return s.Id
}

type AppUpdateReq struct {
	Id           int                  `uri:"id" comment:"编码"` // 编码
	Name         string               `json:"name" comment:"流水线名称"`
	Port         string               `json:"port"`
	Environments []models.Environment `json:"environments" comment:"环境"`
	CodeRepo     string               `json:"codeRepo" comment:"代码仓库地址"`
	ImageUrl     string               `json:"imageUrl" comment:"镜像仓库地址"`
	ImageProject string               `json:"imageProject" comment:"镜像仓库项目"`
	Notify       string               `json:"notify" comment:"飞书通知"`
	JenkinsId    int                  `json:"jenkinsId" comment:"jenkins配置"`
	Status       string               `json:"status" comment:"状态"`
	Remark       string               `json:"remark" comment:"备注"`
	common.ControlBy
}

func (s *AppUpdateReq) Generate(model *models.App) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.Name = s.Name
	model.Port = s.Port
	model.Environments = s.Environments
	model.CodeRepo = s.CodeRepo
	model.Status = s.Status
	model.Remark = s.Remark
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
}

func (s *AppUpdateReq) GetId() interface{} {
	return s.Id
}

// AppGetReq 功能获取请求参数
type AppGetReq struct {
	Id int `uri:"id"`
}

func (s *AppGetReq) GetId() interface{} {
	return s.Id
}

// AppDeleteReq 功能删除请求参数
type AppDeleteReq struct {
	Ids []int `json:"ids"`
}

func (s *AppDeleteReq) GetId() interface{} {
	return s.Ids
}
